/* Copyright 2003, 2008 Gregor Hohpe (http://www.eaipatterns.com) */

using System.Xml;

namespace MessageGateway
{
    /* 
     * Superclass for most of the messaging component implementations. In its default behavior
     * the filter simply forwards all incoming messages.
     */
	public class SimpleFilter
	{
        protected IMessageReceiver input;
        protected IMessageSender output;

        /* Any messaging component requires an input and output channel
         */
        public SimpleFilter(IMessageReceiver input, IMessageSender output)
        {
            this.input = input;
            this.output = output;
            input.AddEventHandler(new OnMsgEvent(OnMessage));
            input.Activate();
        }

        /* Convenience constructor to pass in channel names as strings plus the factory
         * for the desired transport
         */
        public SimpleFilter(string inputQueue, string outputQueue, IMessageGatewayFactory factory) :
            this (factory.GetReceiverInstance(inputQueue), factory.GetSenderInstance(outputQueue))
        { }

        /* Allows other components, e.g. user interface to be notified of incoming
         * messages.
         */
        public void AddEventHandler(OnMsgEvent handler)
        {
            input.AddEventHandler(handler);
        }

        /* The default implementation simply forwards any incoming message.
         */
        protected virtual void OnMessage(XmlDocument message)
        {
            output.Send(message);
        }
	}
}